package com.sand.controller;

import com.sand.dto.Owner.signSelect;
import com.sand.dto.Shopping.AddShoppingDto;
import com.sand.dto.Shopping.SelectShoppingDto;
import com.sand.result.PageResult;
import com.sand.result.ResponseResult;
import com.sand.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sand.entity.shopping.shopping;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Autowired
    private ShoppingService shoppingService;


    //新增或修改商品(完成)
    @PostMapping("/AddOrUpdate")
    public ResponseResult<String> add(@RequestBody AddShoppingDto addShoppingDto) {

        String str = shoppingService.add(addShoppingDto);

        return new ResponseResult<String>().hint(str);
    }

    //根据多个id查询商品(完成)
    @PostMapping("/select")
    public ResponseResult<List<shopping>> select(@RequestBody Integer[] ids) {

        ArrayList<shopping> list = shoppingService.select(ids);

        return new ResponseResult<List<shopping>>().okResult(list);
    }


    //根据条件查询商品(完成)
    @PostMapping("/list")
    private PageResult<List<shopping>> getList(@RequestBody SelectShoppingDto shoppingDto) {

        List<shopping> list = shoppingService.getList(shoppingDto);

        return new PageResult<List<shopping>>().ok(list, list.size());
    }


    //上下架商品(完成)
    @GetMapping("/updown")
    private ResponseResult<Integer> updown(Integer id, Integer shoppingId) {

        Integer i = shoppingService.updown(id, shoppingId);

        return new ResponseResult<Integer>().okResult(i);
    }


    //计算商品材料费用
    @PostMapping("/materialsmoney")
    private ResponseResult<Integer> MaterialsMoney(@RequestBody List<signSelect> signSelects) {
        Integer money = shoppingService.materialsmoney(signSelects);
        return new ResponseResult<Integer>().ok(money);
    }


    //根据id查询商品(完成)
    @PostMapping("/getO")
    private ResponseResult<shopping> getO(@RequestBody Integer shoppingid) {
        shopping shopping = shoppingService.getO(shoppingid);
        return new ResponseResult<shopping>().ok(shopping);
    }


}
